
package com.scs.application.modules.upms.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.scs.application.core.model.CommonResult;
import com.scs.application.core.model.PageResult;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.mvc.controller.ImAndExAbleController;
import com.scs.application.core.utils.BeanValidators;
import com.scs.application.modules.upms.entity.SerialOwner;
import com.scs.application.modules.upms.request.SerialOwnerQueryRequest;
import com.scs.application.modules.upms.service.SerialOwnerService;
import lombok.AllArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
* 编号规则主体 controller
*/
@AllArgsConstructor
@RestController
@RequestMapping("/upms/serial_owners")
public class SerialOwnerController extends CrudController<SerialOwner,String,SerialOwnerService> implements ImAndExAbleController<SerialOwnerQueryRequest> {


    @GetMapping("/page")
    public PageResult page(SerialOwnerQueryRequest request) {
        IPage page = service.page(request);
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    @GetMapping("/list")
    public CommonResult list(SerialOwnerQueryRequest request) {
        List<SerialOwner> list = service.list(request);
        return CommonResult.ok("查询成功", list);
    }

    @PutMapping("/batch_save")
    public CommonResult batchSave(@RequestBody @Valid SerialOwner[] children) {
        Assert.notEmpty(children, "至少有一条主体数据");
        BeanValidators.validateArrayWithException(validator, children);

        boolean bSuccess = service.saveOrUpdateBatch(Lists.newArrayList(children));
        return bSuccess ? CommonResult.ok("保存成功") : CommonResult.error("保存失败");
    }


}
